/*
 * This option the image path, word info and quote info are passed as "datain" from the JSP.
 * Methods below parse the datain based on the delimeter.
 * The format of datain is:
 * imagepath       | hint1*word1,hint2*word2,hint3*word3 | hint1*quote1,hint2*quote2,hint3*quote3
 * <- Image path ->  <----------- word info ------------>  <------------ quote info ------------>
 * 
 */

var imagepath;
var word_data;
var quote_data;
var selected;
var hint;

var alpha=new Array();
var alpha_index=0;

var bravo=new Array();
var bravo_index=0;

var running=0;
var failnum=0;
var advising=0;

function dataReset() {
	running=0;
	failnum=0;
	advising=0;
	bravo_index=0;
	alpha_index=0;
	document.all.word.value='';
}

var praisings = new Array("You are rocking!",
                          "Excellent!",
                          "Fantastic!",
                          "Superb!",
                          "Sweet!",
                          "Good Job!",
                          "Magnificient!");

  	function processOperation(form, datain) {
  		//alert('Change of operation');
  		document.getElementById("hint").innerHTML = '';
  		restart(form, datain);
  	}
          
	function getCheckedRadio(radiobtnname) {
		var selectedval = "";
        var radioButtons = document.getElementsByName(radiobtnname);
		
        for (var x = 0; x < radioButtons.length; x ++) {
          if (radioButtons[x].checked) {
          selectedval = radioButtons[x].value;
            //alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
          }
        }
        return selectedval;
	}


	function pick(datain) {
		//alert('Check 3');
		var choice="";
		var blank=0;
	
		for(i=0; i<selected.length; i++)
		{
			t=0;
			for(j=0; j<=alpha_index; j++) 
				if(selected.charAt(i)==alpha[j] || selected.charAt(i)==alpha[j].toLowerCase()) t=1;
	   
			if(t) choice+=selected.charAt(i)+" ";
			else 
			{
			  if(selected.charAt(i)==' ')
			  {
			  	choice += "  ";
			  }
			  else
			  {
			        choice+="_ ";
				blank=1;
			  }
			}
		}
		//alert('Check 4:');
		//alert('From Pick function');
		//checks();
		//alert('Choice: ' + choice + ', Hint: ' + hint);
		//document.all.word.value=choice;
		document.hmaform.word.value=choice;
		//alert('Choice: ' + choice + 'Display: ' + hint);
		document.getElementById("hint").innerHTML = hint;
		if(!blank)
		{
			praisingindex=Math.floor(Math.random()*praisings.length);
	        alert(praisings[praisingindex] + ' ... You Win! ... Try Next');
			document.hangman.score.value++;
	        //document.all.score.value++;
			running=0;
			//alert('Next word starts. Running: ' + running);
			new_word(document.all, datain);
		}
	}

	function checks() {
		alert('document.hmaform.scores.value: ' + document.hmaform.tried.value);
		alert('document.hmaform.lives.value: ' + document.hmaform.correct.value);
		
		alert('document.hmaform.tried.value: ' + document.hmaform.tried.value);
		alert('document.hmaform.correct.value: ' + document.hmaform.correct.value);
		
		alert('document.hmaform.word.value: ' + document.hmaform.word.value);
		//alert('document.all.word.value: ' + document.hmaform.word.value);
		
	}

	function new_word(form, datain) {
		//alert('new_word: Check1');
		processData(datain);
    
		var operation = getCheckedRadio("operation");
	    if(operation == 'quote') {
	        words = quote_data.split(",");
	    }
	    else {
	    	words = word_data.split(",");
	    }
	    //alert('new_word Check2: Running: ' + running);
    
		if(!running) {
			running=1;
			failnum=0;
			form.lives.value=failnum;
			form.tried.value="";
			form.correct.value="";    
			form.word.value="";
			//index=Math.round(Math.random()*10000) % 100;
	        index=Math.floor(Math.random()*words.length);
	        //alert('New Index: ' + index);
	        var selected_info = words[index].split("*");
	        selected = trimAll(selected_info[1]);
	        hint = trimAll(selected_info[0]);
	        //alert('From new_word: Operation: ' + operation + ', Selected: ' + selected + ', Hint: ' + hint);
			alpha[0]=selected.charAt(0);
			alpha[1]=selected.charAt(selected.length-1);
			alpha_index=1;
			bravo[0]=selected.charAt(0);
			bravo[1]=selected.charAt(selected.length-1);
			bravo_index=1;
			//alert('Check 2');
			pick(datain);
		}
		else
			advise("Quote already in play!");
		
		document.all.res.disabled=false;
		//document.hangman.hm.src="../image/hangman/hmstart3.gif";
		//eval("document.hm.src='" + imagepath + "hmstart3.gif'");
		eval("document.hangman.hm.src='" + imagepath + "hmstart3.gif'");
	  
		for(var i=65; i<=90; i++)
			document.getElementById(String.fromCharCode(i)).style.backgroundColor='#D3D3D3';
	}

	function processData(datain) {
		var data_array = datain.split("|");
		imagepath = data_array[0];
		word_data = data_array[1];
		quote_data = data_array[2];
		//alert('Image Path: ' + imagepath + ', words: ' + word_data + ', quotes: ' + quote_data);
	}

	function trimAll(sString) {
		while (sString.substring(0,1) == ' ') {
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ') {
			sString = sString.substring(0,sString.length-1);
		}
		return sString;
	} 

	function restart(form, datain) {   
	   
		processData(datain);
		
		var operation = getCheckedRadio("operation");
	    
	    if(operation == 'quote') {
	        words = quote_data.split(",");
	    }
	    else {
	    	words = word_data.split(",");
	    }
	            
	    running=1;
		failnum=0;
		form.lives.value=failnum;
		form.tried.value="";
		form.correct.value="";    
		form.word.value="";
	    //index=Math.round(Math.random()*10000) % 100;
	    index=Math.floor(Math.random()*words.length);
	    var selected_info = words[index].split("*");
	    selected = trimAll(selected_info[1]);
	    hint = trimAll(selected_info[0]);
	    //alert('From restart: Operation: ' + operation + ', Selected: ' + selected + ', Hint: ' + hint);
		alpha[0]=selected.charAt(0);
		alpha[1]=selected.charAt(selected.length-1);
		alpha_index=1;
		bravo[0]=selected.charAt(0);
		bravo[1]=selected.charAt(selected.length-1);
		bravo_index=1;
		pick(datain);
		//document.hangman.hm.src="../image/hangman/hmstart3.gif";  
		//eval("document.hm.src='" + imagepath + "hmstart3.gif'");
		eval("document.hangman.hm.src='" + imagepath + "hmstart3.gif'");
	    
		for(var i=65; i<=90; i++)
			document.getElementById(String.fromCharCode(i)).style.backgroundColor='#D3D3D3';
	}


	function seek(letter, datain) {
		alert('From seek, letter: ' + letter + '; datain: ' + datain + '; Form name: ' + getFormName());
		checks();
		
		
		processData(datain); //????
		
		if(!running)
			alert(".....Click 'New' to start !");
		else
		{
			t=0;
			for(i=0; i<=bravo_index; i++)
				if (bravo[i]==letter || bravo[i]==letter.toLowerCase()) t=1;
	
			if(!t) 
			{
				bravo_index++;
				bravo[bravo_index]=letter;
		    
				for(i=0;i<selected.length;i++)
					if(selected.charAt(i)==letter || selected.charAt(i)==letter.toLowerCase()) t=1;
			
				if(t)
				{
					alpha_index++;
					alpha[alpha_index]=letter;
					document.hangman.correct.value+=letter+" ";	     
					//document.all.correct.value+=letter+" ";	
			      		document.getElementById(letter).style.backgroundColor='#FFF8DC';	
				}
				else
				{
					failnum++;
					document.hangman.tried.value+=letter+" ";	 
					//document.all.tried.value+=letter+" ";
					//eval("document.hm.src=\"" + imagepath + "hm" + failnum + ".gif\"");
					eval("document.hangman.hm.src='" + imagepath + "hm" + failnum + ".gif'");
			      		document.getElementById(letter).style.backgroundColor='#CCD8FF';
				}
				
				document.hangman.lives.value=failnum;
				//document.all.lives.value=failnum;
	
				if(failnum==10) 
				{
					alert('You lose - Try again! \n The correct answer is: --> \n' + selected);
					document.hangman.score.value--;
					//document.all.score.value--;
					running=0;
					new_word(document.all, datain);
				}
				else pick(datain);
			}
			else
				advise("Letter "+letter+" is already used!");
		  }
	  
	}

	function advise(msg) {
		if(!advising)
		{
			advising=-1;
			savetext=document.hangman.correct.value;  
			//savetext=document.all.correct.value;
			document.hangman.correct.value=msg;
			//document.all.correct.value=msg;
			//window.setTimeout("document.hangman.correct.value=savetext; advising=0;",500);
			window.setTimeout("document.all.correct.value=savetext; advising=0;",500);
		}
	}

